﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using Eniux.Windows.ThreeDEngine.Helper;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    ///沿指定路径拉伸截面
    /// </summary>
    public class ExtrudedVisual3D : MeshElement3D
    {
        // http://linas.org/gle/
        // http://sharpmap.codeplex.com/Thread/View.aspx?ThreadId=18864
        #region 注册属性

        #region Path
        /// <summary>
        /// 三维路径属性
        /// </summary>
        public static readonly DependencyProperty PathProperty = DependencyProperty.Register(
            "Path",
            typeof(IList<Point3D>),
            typeof(ExtrudedVisual3D),
            new UIPropertyMetadata(new Point3DCollection(), GeometryChanged));

        /// <summary>
        /// 获取或设置三维路径
        /// </summary>
        public IList<Point3D> Path
        {
            get { return (IList<Point3D>)GetValue(PathProperty); }
            set { SetValue(PathProperty, value); }
        }
        #endregion

        #region IsPathClosed
        /// <summary>
        /// 路径是否闭合属性
        /// </summary>
        public static readonly DependencyProperty IsPathClosedProperty = DependencyProperty.Register(
            "IsPathClosed", typeof(bool), typeof(ExtrudedVisual3D), new UIPropertyMetadata(false, GeometryChanged));

        /// <summary>
        /// 获取或设置路径是否闭合
        /// </summary>
        public bool IsPathClosed
        {
            get { return (bool)this.GetValue(IsPathClosedProperty); }
            set { this.SetValue(IsPathClosedProperty, value); }
        } 
        #endregion

        #region Section
        /// <summary>
        /// 节点属性
        /// </summary>
        public static readonly DependencyProperty SectionProperty = DependencyProperty.Register(
            "Section",
            typeof(IList<Point>),
            typeof(ExtrudedVisual3D),
            new UIPropertyMetadata(new PointCollection(), GeometryChanged));

        /// <summary>
        /// 获取或设置节点
        /// </summary>
        public IList<Point> Section
        {
            get { return (IList<Point>)GetValue(SectionProperty); }
            set { SetValue(SectionProperty, value); }
        }
        #endregion

        #region IsSectionClosed
        /// <summary>
        /// 节点是否闭合属性
        /// </summary>
        public static readonly DependencyProperty IsSectionClosedProperty =
            DependencyProperty.Register(
                "IsSectionClosed", typeof(bool), typeof(ExtrudedVisual3D), new UIPropertyMetadata(true, GeometryChanged));

        /// <summary>
        /// 获取或设置节点是否闭合
        /// </summary>
        public bool IsSectionClosed
        {
            get { return (bool)GetValue(IsSectionClosedProperty); }
            set { SetValue(IsSectionClosedProperty, value); }
        } 
        #endregion

        #region UpVector
        /// <summary>
        /// 向上向量属性.
        /// </summary>
        public static readonly DependencyProperty UpVectorProperty = DependencyProperty.Register(
            "UpVector",
            typeof(Vector3D),
            typeof(ExtrudedVisual3D),
            new UIPropertyMetadata(new Vector3D(0, 0, 1), GeometryChanged));

        /// <summary>
        /// 获取或设置向上向量
        /// </summary>
        public Vector3D UpVector
        {
            get { return (Vector3D)GetValue(UpVectorProperty); }
            set { SetValue(UpVectorProperty, value); }
        }
        #endregion 

        #endregion


        #region 方法
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new MeshBuilder();
            builder.AddTube(this.Path, null, null, this.Section, this.IsPathClosed, this.IsSectionClosed);
            return builder.ToMesh();
        }

        #endregion
    }
}
